package test.gen.core;

import gen.core.managers.TemplateManager;
import gen.core.models.Template;
import gen.core.utils.GenUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.util.Properties;

import junit.framework.TestCase;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.lang.ArrayUtils;

public class TemplateManagerTestCase extends TestCase {
	private Connection conn;
	
	public void setUp(){
		Properties aProperties = new Properties();
		aProperties.setProperty("driver", "org.sqlite.JDBC");
		aProperties.setProperty("url", "jdbc:sqlite:test.db");
		conn = GenUtils.createConnection(aProperties);		
	}
	
	public void tearDown() throws Exception{
		DbUtils.close(conn);
	}
	
	public void testList() throws Exception{
		TemplateManager aTemplateManager = new TemplateManager();
		System.out.println(ArrayUtils.toString(aTemplateManager.list(conn, "test", "test")));;
	}
	
	public void testSave() throws Exception{
		TemplateManager aTemplateManager = new TemplateManager();
		
		Template aTemplate = new Template();
		aTemplate.setId("477dcad3-484e-467a-9f88-887f5e461a04");
		aTemplate.setOwner("test");
		aTemplate.setCatagory("test");
		aTemplate.setContent("test11111");
		
		aTemplateManager.save(conn, aTemplate);
		conn.commit();		
	}
	
	public void testLoad() throws Exception{
		TemplateManager aTemplateManager = new TemplateManager();
		Template aTemplate = aTemplateManager.load(conn, "477dcad3-484e-467a-9f88-887f5e461a04");
		System.out.println(aTemplate);
	}
	
	public void testLoadAndSave() throws Exception{
		TemplateManager aTemplateManager = new TemplateManager();
		Template aTemplate = aTemplateManager.load(conn, "477dcad3-484e-467a-9f88-887f5e461a04");
		aTemplate.setName("loadandsave");
		aTemplateManager.save(conn, aTemplate);
		System.out.println(aTemplateManager.load(conn, "477dcad3-484e-467a-9f88-887f5e461a04"));
	}
	
	public void testDelete() throws Exception{
		TemplateManager aTemplateManager = new TemplateManager();
		Template aTemplate = new Template();
		aTemplate.setOwner("test");
		aTemplate.setCatagory("test");
		aTemplate.setContent("test11111");
		
		aTemplateManager.save(conn, aTemplate);
		System.out.println("a template was saved! it's id is "+aTemplate.getId());
		
		aTemplateManager.delete(conn, aTemplate.getId());
	}
	
	public void testExportJson() throws Exception{
		TemplateManager aTemplateManager = new TemplateManager();
		Template aTemplate = new Template();
		aTemplate.setOwner("test");
		aTemplate.setCatagory("test");
		aTemplate.setContent("test11111");
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		aTemplateManager.exportJson(baos, aTemplate);
		System.out.println(baos.toString());
	}
	
	public void testImportJson() throws Exception{
		TemplateManager aTemplateManager = new TemplateManager();
		String jsonstr ="[{\"content\":\"test11111\",\"catagory\":\"test\",\"owner\":\"test\",\"name\":\"one\"}, {\"content\":\"test32\",\"catagory\":\"test23\",\"owner\":\"me\",\"name\":\"two\"}]";
		ByteArrayInputStream bais = new ByteArrayInputStream(jsonstr.getBytes());
		Template[] array = aTemplateManager.importJson(bais);
		for(Template aTemplate : array){
			System.out.println(aTemplate);
		}
	}
}
